Test Series - Data Structure

Test Number 54/115

Q: Select the code snippet which performs in-order traversal.
A. public void inorder(Tree root) { System.out.println(root.data); inorder(root.left); inorder(root.right); }
B. public void inorder(Tree root) { inorder(root.left); System.out.println(root.data); inorder(root.right); }
C. public void inorder(Tree root) { System.out.println(root.data); inorder(root.right); inorder(root.left); }
D. public void inorder(Tree root) { inorder(root.right); inorder(root.left); System.out.println(root.data); }
Solution: In-order traversal follows LNR(Left-Node-Right).
Q: Select the code snippet which performs level-order traversal.
A. public static void levelOrder(Tree root) { Queue queue=new LinkedList(); queue.add(root); while(!queue.isEmpty()) { Node tempNode=queue.poll(); System.out.println("%d ",tempNode.data); if(tempNode.left!=null) queue.add(tempNode.left); if(tempNode.right!=null) queue.add(tempNode.right); } }
B. public static void levelOrder(Tree root) { Queue queue=new LinkedList(); queue.add(root); while(!queue.isEmpty()) { Node tempNode=queue.poll(); System.out.println("%d ",tempNode.data); if(tempNode.left!=null) queue.add(tempNode.right); if(tempNode.right!=null) queue.add(tempNode.left); } }
C. public static void levelOrder(Tree root) { Queue queue=new LinkedList(); queue.add(root); while(!queue.isEmpty()) { Node tempNode=queue.poll(); System.out.println("%d ",tempNode.data); if(tempNode.right!=null) queue.add(tempNode.left); if(tempNode.left!=null) queue.add(tempNode.right); } }
D. public static void levelOrder(Tree root) { Queue queue=new LinkedList(); queue.add(root); while(!queue.isEmpty()) { Node tempNode=queue.poll(); System.out.println("%d ",tempNode.data); if(tempNode.right!=null) queue.add(tempNode.left.left); if(tempNode.left!=null) queue.add(tempNode.right.right); } }
Solution: Firstly add the root node to the queue. Then for all the remaining nodes, pop the front end of the queue and print it, add the left and right children of the popped node to the queue.
Q: What is the space complexity of the in-order traversal in the recursive fashion? (d is the tree depth and n is the number of nodes)
A. O(1)
B. O(nlogd)
C. O(logd)
D. O(d)
Solution: In the worst case we have d stack frames in the recursive call, hence the complexity is O(d).
Q: What is the time complexity of level order traversal?
A. O(1)
B. O(n)
C. O(logn)
D. O(nlogn)
Solution: Since you have to go through all the nodes, the complexity becomes O(n).
Q: Which of the following graph traversals closely imitates level order traversal of a binary tree?
A. Depth First Search
B. Breadth First Search
C. Depth & Breadth First Search
D. Binary Search
Solution: Both level order tree traversal and breadth first graph traversal follow the principle that visit your neighbors first and then move on to further nodes.
Q: In a binary search tree, which of the following traversals would print the numbers in the ascending order?
A. Level-order traversal
B. Pre-order traversal
C. Post-order traversal
D. In-order traversal
Solution: In a binary search tree, a node’s left child is always lesser than the node and right child is greater than the node, hence an in-order traversal would print them in a non decreasing fashion.

You Have Score    /6